<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <!--以上代码告诉IE浏览器，IE8/9及以后的版本都会以最高版本IE来渲染页面-->
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!--显示窗口 宽度是客户端的屏幕宽度（就是 满屏 ！），显示的文字和图形的初始比例是1.0-->
    <title>信息管理系统</title>
    <link href="../static/css/bootstrap.css" rel="stylesheet" type="text/css"/>
    <link href="../static/css/font-awesome-4.7.0/css/font-awesome.css" rel="stylesheet" type="text/css"/>
    <link rel="stylesheet" type="text/css" href="../static/css/login.css"/>
    <script src="../static/js/jquery-1.11.0.min.js"></script>
    <script src="../static/js/bootstrap.min.js"></script>
    <script src="../static/js/jquery.cookie.js"></script>
</head>
<body style="background: #E0E0E0;">
<a href="?lang=zh_CN"><spring:message code="language.cn"/></a> &nbsp;&nbsp;&nbsp;
<a href="?lang=en_US"><spring:message code="language.en"/></a>
<div id="Wrapper">
    <div id="bouncingBalls">
        <div class="container ">
            <div class="row">
                <!--水平表单 -->
                <form class="form-horizontal " id="loginForm">
                    <span class="heading"><spring:message code="manage"/></span>
                    <div class="form-group">
                        <input type="text" class="form-control" name="userName" id="username"
                               placeholder="<spring:message code="username"/>"/>
                        <i class="fa fa-user"></i>
                        <span id="sone" style="color: red;"></span>
                    </div>
                    <div class="form-group">
                        <input type="password" class="form-control" name="passWord" id="password"
                               placeholder="<spring:message code="password"/>"/>
                        <i class="fa fa-lock"></i>
                        <a><span class="fa fa-question-circle" data-toggle="modal" data-target="#myModal"></span></a>
                        <span id="stwo" style="color: red;"></span>
                    </div>
                    <div class="form-group">
                        <label class="lab"><spring:message code="code"/>:</label>
                        <input type="text" value="" placeholder="<spring:message code="code"/>" class="input-val"/>
                        <canvas id="canvas" width="100" height="40"></canvas>
                        <span id="sp"></span>
                    </div>
                    <p id="msg" style="color: red;"></p>
                    <div class="form-group">
                        <div class="main-checkbox">
                            <input type="checkbox" name="rememberMe" id="remember" checked="checked"/>
                            <label for="remember"></label>
                        </div>
                        <span class="text"><spring:message code="remember.me"/></span>
                        <input type="button" class="btn btn-info" value="<spring:message code="login"/>" id="btn"/>
                        <a class="btn btn-default" href="/register/index"
                           style="background-color: #ebebeb;color: black;margin-right:10px;"><spring:message
                                code="register"/></a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="width:300px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel"><spring:message code="forgetPassword"/></h4>
            </div>
            <div class="modal-body">
                <div class="login-box">
                    <!-- /.login-logo -->
                    <div class="card">
                        <div class="card-body login-card-body">
                            <span class="result" style="color:red;"></span>
                            <form method="post" id="ForgetPassword" onkeydown="if(event.keyCode==13){return false;}">
                                    <div class="form-group has-feedback">
                                        <span style="margin-top:10px" class="fa fa-phone form-control-feedback"></span>
                                        <input style="margin-top:10px" id="phoneNumber" name="phoneNumber" type="text"
                                               class="form-control"
                                               placeholder="<spring:message code="telephoneNumber"/>"/>
                                        <input type="button" id="btn1" class="btn" style="margin-top: 20px;"
                                               value="<spring:message code="getCode"/>"/>
                                        <span class="result_1"
                                              style="float:right; margin-right: 10px; margin-top: 30px; color:red;"></span>
                                    </div>
                                <!-- 表单回车不自动提交 -->

                                <div class="form-group has-feedback">
                                    <span style="margin-top:10px" class="fa fa-envelope form-control-feedback"></span>
                                    <input id="msga" name="msg" type="text" class="form-control"
                                           placeholder="<spring:message code="code"/>"/>
                                </div>
                                <div class="form-group has-feedback">
                                    <span style="margin-top:10px" class="fa fa-lock form-control-feedback"></span>
                                    <input id="newPassword_1" name="newPassword_1" type="password" class="form-control"
                                           placeholder="<spring:message code="password"/>"/>
                                </div>
                                <div class="form-group has-feedback">
                                    <span style="margin-top:10px" class="fa fa-check form-control-feedback"></span>
                                    <input id="newPassword_2" name="newPassword_2" type="password" class="form-control"
                                           placeholder="<spring:message code="confirm"/>"/>

                                </div>
                                <div class="row">
                                    <!-- /.col -->
                                    <div class="col-12">
                                        <button type="button" class="btn btn-primary btn-block btn-flat" id="btn2">
                                            <spring:message code="verify"/></button>
                                    </div>
                                    <!-- /.col -->
                                </div>
                            </form>

                            <div class="social-auth-links text-center mb-3">
                            </div>
                            <!-- /.login-card-body -->
                        </div>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal -->
        </div>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        if ($.cookie("remember") == "true") {
            $("#remember").attr("checked", true);
            $("#username").val($.cookie("username"));
            $("#password").val($.cookie("password"));
        }
        ;

        $("#username").blur(function () {
            $("#sone").html('');
            if ($("#username").val() == '') {
                $("#sone").html('<spring:message code="accountNotNull"/>');
            }
        });

        $("#password").blur(function () {
            $("#stwo").html('');
            if ($("#password").val() == '') {
                $("#stwo").html('<spring:message code="passwordNotNull"/>');
            }
        });

        var show_num = [];
        draw(show_num);

        $("#canvas").on('click', function () {
            draw(show_num);
        })
        $("#btn").click(function () {
            var userName = $("#username").val();
            var passWord = $("#password").val();

            $("#sp").html('');
            var val = $(".input-val").val().toLowerCase();
            var num = show_num.join("");

            if (val == '') {
                $("#sp").text("<spring:message code="codeNotNull"/>");
            }
            if (val != num && val != '') {
                $("#sp").text("<spring:message code="codeError"/>");
                $(".input-val").val('');
                draw(show_num);
            }

            if ($("#username").val() == '') {
                $("#sone").html('<spring:message code="accountNotNull"/>');
            }

            if ($("#password").val() == '') {
                $("#stwo").html('<spring:message code="passwordNotNull"/>');
            }

            var message = {
                userName: userName,
                passWord: passWord,
            };
            if ($("#sp").html() == '' && userName != '' && passWord != '') {
                $.ajax({
                    async: false,
                    type: "post",
                    url: "/demo/subLogin",
                    dataType: "json",
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(message),
                    success: function (data) {

                        if (data.statue == 1) {
                            Save();
                            window.location.href = "/main/show";
                        } else {
                            $("#sp").html('');
                            draw(show_num);
                            $("#username").val("");
                            $("#password").val("");
                            $(".input-val").val('');
                            $("#msg").text(data.message);

                        }
                    }
                });
            }
        });

    });

    function draw(show_num) {
        var canvas_width = $('#canvas').width();
        var canvas_height = $('#canvas').height();
        var canvas = document.getElementById("canvas");//获取到canvas的对象，演员
        var context = canvas.getContext("2d");//获取到canvas画图的环境，演员表演的舞台
        canvas.width = canvas_width;
        canvas.height = canvas_height;
        var sCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0";
        var aCode = sCode.split(",");
        var aLength = aCode.length;//获取到数组的长度

        for (var i = 0; i <= 3; i++) {
            var j = Math.floor(Math.random() * aLength);//获取到随机的索引值
            var deg = Math.random() * 30 * Math.PI / 180;//产生0~30之间的随机弧度
            var txt = aCode[j];//得到随机的一个内容
            show_num[i] = txt.toLowerCase();
            var x = 10 + i * 20;//文字在canvas上的x坐标
            var y = 20 + Math.random() * 8;//文字在canvas上的y坐标
            context.font = "bold 23px 微软雅黑";

            context.translate(x, y);
            context.rotate(deg);

            context.fillStyle = randomColor();
            context.fillText(txt, 0, 0);

            context.rotate(-deg);
            context.translate(-x, -y);
        }
        for (var i = 0; i <= 5; i++) { //验证码上显示线条
            context.strokeStyle = randomColor();
            context.beginPath();
            context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
            context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
            context.stroke();
        }
        for (var i = 0; i <= 30; i++) { //验证码上显示小点
            context.strokeStyle = randomColor();
            context.beginPath();
            var x = Math.random() * canvas_width;
            var y = Math.random() * canvas_height;
            context.moveTo(x, y);
            context.lineTo(x + 1, y + 1);
            context.stroke();
        }
    }

    function randomColor() {//得到随机的颜色值
        var r = Math.floor(Math.random() * 256);
        var g = Math.floor(Math.random() * 256);
        var b = Math.floor(Math.random() * 256);
        return "rgb(" + r + "," + g + "," + b + ")";
    }

    //记住用户名密码
    function Save() {
        if ($("#remember").is(":checked")) {
            var str_username = $("#username").val();
            var str_password = $("#password").val();
            $.cookie("remember", "true", {expires: 7}); //存储一个带7天期限的cookie
            $.cookie("username", str_username, {expires: 7});
            $.cookie("password", str_password, {expires: 7});
        }
        else {
            $.cookie("remember", "false", {expire: -1});
            $.cookie("username", "", {expires: -1});
            $.cookie("password", "", {expires: -1});
        }
    };
    var wait = 60;
    var result;
    document.getElementById("btn1").onclick = function () {
        wait = 60;
        time(this);
    }
    document.getElementById("btn2").onclick = function () {
        ForgetPassword();
    }

    function ForgetPassword() {
        if($("msga").val() != ''){
        if ($("#newPassword_1").val() != '' && $("#newPassword_2").val() != '') {
            if ($("#newPassword_1").val() == $("#newPassword_2").val()) {
                $.ajax({
                    cache: true,
                    type: "POST",
                    url: "/register/forgetPWD",
                    data: $('#ForgetPassword').serialize(),// 你的formid
                    async: false,
                    error: function (request) {
                        alert("Connection error:" + request.error);
                    },
                    success: function (r) {
                        var data = JSON.parse(r);
                        if (data.statue == 1) {
                            alert(data.message + "  点击确定返回登陆界面");
                            window.location.replace("/demo/login");
                        } else {
                            $('.result_1').text(data.message);
                        }
                    }
                });
            } else {
                $('.result_1').text('<spring:message code="passwordsDifferent"/>');
            }
        } else {
            $('.result_1').text('<spring:message code="passwordNotNull"/>');
        }
        }else{
            $('.result_1').text('<spring:message code="codeNotNull"/>');
        }
    }

    var status;

    function time(o) {
        var a = $("#phoneNumber").val();
        message = {
            phoneNumber : a
        }
        if (wait == 0) {
            o.removeAttribute("disabled");
            o.value = "免费获取验证码";
        } else {
            if (wait == 60) {
                $.ajax({
                    cache: true,
                    type: "POST",
                    url: "/register/sendCode",
                    data: message,
                    async: false,
                    error: function (request) {
                        alert("Connection error:" + request.error);
                    },
                    success: function (data) {
                        result = JSON.parse(data);
                        $('.result').text(result.message);
                        status = result.statue;
                    }
                });
            }
            if (status != 1) {
                o.removeAttribute("disabled");
                o.value = '<spring:message code="getCode"/>';
            } else {
                o.setAttribute("disabled", true);
                o.value = wait + '<spring:message code="Resend"/>';
                wait--;
                setTimeout(function () {
                    time(o)
                }, 1000)
            }
        }
    }

    $(function () {
        $('input').iCheck({
            checkboxClass: 'icheckbox_square-blue',
            radioClass: 'iradio_square-blue',
            increaseArea: '20%' // optional
        })
    })
    //密码验证
    var pass = /^(?![_@#!?$%^&*()+=-`~.,;/]+$)(?![0-9]+$)[0-9A-Za-z_@#!?$%^&*()+=-`~.,;/]{8,16}$/;
    $("#newPassword_2").keyup(function () {
        if (!pass.test($("#newPassword_2").val())) {
            $(".result").text('<spring:message code="passwordFormat"/> ').css('color', 'red');
            $("#button").attr("disabled", true);
        } else {
            $(".result").text("");
            $("#button").attr("disabled", false);
        }
    });
    //密码验证
    var pass = /^(?![_@#!?$%^&*()+=-`~.,;/]+$)(?![0-9]+$)[0-9A-Za-z_@#!?$%^&*()+=-`~.,;/]{8,16}$/;
    $("#newPassword_1").keyup(function () {
        if (!pass.test($("#newPassword_1").val())) {
            $(".result").text('<spring:message code="passwordFormat"/> ').css('color', 'red');
            $("#button").attr("disabled", true);
        } else {
            $(".result").text("");
            $("#button").attr("disabled", false);
        }
    });
    //电话验证
    var tn =/(\d{15})|(\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$/;
    $("#phoneNumber").keyup(function() {
        if(!tn.test($("#phoneNumber").val())) {
            $(".result").text('<spring:message code="telFormat"/> ').css('color','red');
        } else {
            $(".result").text("");
        }
    });
</script>
</body>
</html>
